package com.jiuzhe.commercial.ui.house.editor;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.adapter.HouseStayListAdapter;
import com.jiuzhe.commercial.base.BaseActivity;
import com.jiuzhe.commercial.bean.HouseListBean;
import com.jiuzhe.commercial.constant.Constants;
import com.jiuzhe.commercial.http.CallBack;
import com.jiuzhe.commercial.http.HttpClient;
import com.jiuzhe.commercial.utils.RxBus;
import com.jiuzhe.commercial.utils.SPUtil;
import com.jiuzhe.commercial.utils.UIUtils;
import com.jiuzhe.commercial.view.WrapContentLinearLayoutManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by huanghao on 2018/6/7.
 */

public class HomeStayListActivity extends BaseActivity{

    @BindView(R.id.listview)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.iv_not)
    ImageView ivNot;
    private HouseStayListAdapter mHouseAdapter;
    private List<HouseListBean> list = new ArrayList<>();
    private List<HouseListBean> list2 = new ArrayList<>();
    private Subscription mSubscription;
    private int page = 1;
    private int size = 20;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_homestay_list);
        setTitle("房源");
      //  recyclerView.setLayoutManager(new LinearLayoutManager(HomeStayListActivity.this));
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        //设置Item增加、移除动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        mHouseAdapter = new HouseStayListAdapter(list);
        recyclerView.setAdapter(mHouseAdapter);
        initData();
        loadData2(page,size);
        initRefresh();
    }




    private void initRefresh() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 0;
                        list.clear();
                        loadData2(page,size);
                        refreshLayout.finishRefresh();
                    }
                },1500);
            }
        });

       /* refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mHouseAdapter.getItemCount() > 0) {
                    page++;
                    loadData2(page, size);
                    refreshLayout.finishLoadmore();
                    ivNot.setVisibility(View.GONE);
                } else {
                    refreshLayout.finishLoadmoreWithNoMoreData();//将不会再次触发加载更多事件
                    Toast.makeText(getApplication(), "我是有底线的", Toast.LENGTH_SHORT).show();
                    ivNot.setVisibility(View.GONE);
                }
            }
        });*/
    }

    private void initData() {
        mSubscription = RxBus.getInstance().toObserverable(HouseListBean.class)
                .subscribe(new Action1<HouseListBean>() {
                    @Override
                    public void call(HouseListBean bean) {
                        loadData();
                    }
                });
    }


    private void loadData2(int page, int size) {
        dialog.show();
        HashMap<String, String> map = new HashMap<>();
        map.put("merchantId", SPUtil.getString(HomeStayListActivity.this, Constants.MERCHANTID));
        String token = SPUtil.getString(HomeStayListActivity.this, Constants.TOKEN);
        String id = SPUtil.getString(HomeStayListActivity.this, Constants.ID);
        String autoSignStr = Constants.SKUDETAILS.substring(Constants.SKUDETAILS.lastIndexOf("0/") + 1) + "?token=";
        String signStr = UIUtils.Md5(autoSignStr + token);
        map.put("token", id);
        map.put("sign", signStr);
        map.put("currentPage", page + "");
        map.put("number", size + "");
        HttpClient.post(this, Constants.SKUDETAILS, map, new CallBack<List<HouseListBean>>() {
            @Override
            public void onSuccess(List<HouseListBean> result) {
                dialog.dismiss();
                if (result.size() > 0) {
                    list.addAll(result);
                    list2.addAll(result);
                    int previousSize = list.size();
                    mHouseAdapter.notifyItemRangeRemoved(0, previousSize);
                    mHouseAdapter.notifyItemRangeInserted(0, previousSize);
                   // mHouseAdapter.notifyDataSetChanged();
                    mHouseAdapter.setOnItemClickListener(new HouseStayListAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            Intent intent = new Intent(HomeStayListActivity.this,EditorHomeStayActivity.class);
                            if (list.size() > 0)
                                intent.putExtra("skuid",list.get(position).getSkuId());
                            startActivity(intent);
                        }
                    });
                } else {
                   // ivNot.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFailure(int errorType, String message) {
                super.onFailure(errorType, message);
                dialog.dismiss();
            }
        });
    }

    @Override
    protected void loadData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mSubscription != null ) {
            if(!mSubscription.isUnsubscribed()){
                mSubscription.unsubscribe();
            }
        }
    }
}
